<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <h2>{{id?'编辑':'新增'}}管理员</h2>
    </div>
    <el-form :label-position="labelPosition" label-width="80px" @submit.native.prevent="saveCategory">
      <el-form-item label="用户名" style="width: 50%;margin-left: 20%">
        <el-input v-model="model.username"></el-input>

      </el-form-item>
      <el-form-item label="密码" style="width: 50%;margin-left: 20%">
        <el-input v-model="model.password"></el-input>
      </el-form-item >
      <el-form-item label="昵称" style="width: 50%;margin-left: 20%">
        <el-input v-model="model.nickname"></el-input>
      </el-form-item >
      <el-form-item label="头像" style="width: 50%;margin-left: 20%">
        <el-upload
          class="avatar-uploader"
          :action="$http.defaults.baseURL + '/uploads'"
          :show-file-list="false"
          :headers="getAuthHeaders()"
          :on-success="handleImageSuccess">
          <img v-if="model.avatar" :src="model.avatar" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item style="margin-left: 35%">
        <el-button type="success" round  native-type="submit">提交</el-button>
      </el-form-item>

    </el-form>
  </el-card>
</template>

<script>
  import auth from '../../mixin/index'
  export default {
    name: "AdminUserEdit",
    props:['id'],
    mixins:[auth],
    data(){
      return {
        labelPosition: 'right',
        model:{
          username:'',
          password:'',
          nickname:'',
          avatar:''
        },
      }
    },
    methods:{
      handleImageSuccess(res){
        console.log(res)
        this.model.avatar = res.url
      },
      async saveCategory(){
        let result
        if(this.id){
          result = await this.$http.put(`/rest/admin_users/${this.id}`,this.model)
          this.$message({
            message: `修改管理员${this.model.username}成功`,
            type: 'success'
          });
          this.$router.push('/admin_users/list')
        }else {
          result = await this.$http.post('/rest/admin_users',this.model)
          if(result.status != 200){
            this.$message({
              message: `${result.data}`,
              type: 'warning'
            });
            this.model = Object.assign(this.$data, this.$options.data())
          } else {
            this.$message({
              message: `创建管理员${this.model.username}成功`,
              type: 'success'
            });
            this.$router.push('/admin_users/list')
          }
        }
      },
      async getCategoriesEditData(){
        const res =  await this.$http.get(`/rest/admin_users/${this.id}`)
        this.model = res.data
      },

    },
    created() {
      this.id ? this.getCategoriesEditData():''
    }
  }
</script>

<style lang="scss" scoped>

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    border: 1px dashed #d9d9d9;
    &:hover{
      border-color: #409EFF;
    }
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
